package petstore.web.servlet.recommend;

import petstore.domain.Item;
import petstore.domain.Product;
import petstore.persistence.user.User;
import petstore.persistence.user.UserDAOImpl;
import petstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@WebServlet("/recommend")
public class RecommendServlet extends HttpServlet {
    private String RECOMMEND_PAGE="/WEB-INF/jsp/RecommendPage/RecommendPage.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if(session.getAttribute("uid") != null) {
            int uid = Integer.parseInt(req.getParameter("uid"));

            CatalogService catalogService = new CatalogService();
            UserDAOImpl userDAO = new UserDAOImpl();
            User user = userDAO.FindUserByUser_id(uid);
            double totalFenmu = user.getKey_bird() + user.getKey_bugcat() + user.getKey_cat() + user.getKey_cat() + user.getKey_dog();
            int numCat = Math.min((int) (user.getKey_cat() / totalFenmu * 8.0 + 0.5), 6);
            int numDog = Math.min((int) (user.getKey_dog() / totalFenmu * 8.0 + 0.5), 6);
            int numBird = Math.min((int) (user.getKey_bird() / totalFenmu * 8.0 + 0.5), 6);
            int numPokemon = Math.min((int) (user.getKey_pokemon() / totalFenmu * 8.0 + 0.5), 6);
            int numBugCat = Math.min((int) (user.getKey_bugcat() / totalFenmu * 8.0 + 0.5), 6);
            List<Product> productListCat = catalogService.getProductListBycategoryId("cat");
            List<Product> productListDog = catalogService.getProductListBycategoryId("dog");
            List<Product> productListBird = catalogService.getProductListBycategoryId("bird");
            List<Product> productListPokemon = catalogService.getProductListBycategoryId("pokemon");
            List<Product> productListBugCat = catalogService.getProductListBycategoryId("bugCat");
            List<Product> productList = new ArrayList<>();
            Collections.shuffle(productListCat);
            Collections.shuffle(productListDog);
            Collections.shuffle(productListBird);
            Collections.shuffle(productListPokemon);
            Collections.shuffle(productListBugCat);
            for (int i = 0; i < Math.min(numCat, productListCat.size()); ++i) productList.add(productListCat.get(i));
            for (int i = 0; i < Math.min(numDog, productListDog.size()); ++i) productList.add(productListDog.get(i));
            for (int i = 0; i < Math.min(numBird, productListBird.size()); ++i) productList.add(productListBird.get(i));
            for (int i = 0; i < Math.min(numPokemon, productListPokemon.size()); ++i)
                productList.add(productListPokemon.get(i));
            for (int i = 0; i < Math.min(numBugCat, productListBugCat.size()); ++i)
                productList.add(productListBugCat.get(i));
            for (Product p : productList) {
                System.out.println(p.getName());
            }

            //向jsp传递

            session.setAttribute("productList", productList);
            req.getRequestDispatcher(RECOMMEND_PAGE).forward(req, resp);
        }
        else req.getRequestDispatcher(RECOMMEND_PAGE).forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
